#!/usr/bin/perl - for syntax highlighting
# One argument passed - the way to sort the returned list 

sub mm_form_subscribe_to_portal_populate($) {
my $portal=$_[0];

	my $p2=hex_to_string($portal);
	print STDERR "P1=$p2\n";
	my ($lref,$err)=$::GRI_FRONTEND->get_portal_report_names($p2);
	if(!defined($lref)) {
		print STDERR "Error: mm_form_subscribe_to_portal :: get_portal_report_names returned '$err'.\n";
		return "Error: mm_form_subscribe_to_portal :: $err";
	}
	my @L=();
	if(ref($lref)) {
		@L=@$lref;
	} else {
		@L=split(/,/,$lref);
	}

	#########################################################################
	# lref is the existing report names which are subscribed to.		#
	# We simply show the list of reports in a table.			#
	#########################################################################
	my $rep_dir=$::GRI_FRONTEND->get_global_directory("reports");
	my $dfd;
	opendir($dfd,$rep_dir) or return "Error: Unable to open '$rep_dir' : $!";
	my @R=();
	while($_=readdir($dfd)) {
		next if ! /\.xml$/;
		s/\.xml$//;
		push @R,$_;
	}
	closedir($dfd);

	#########################################################################
	# Now we have the list of file names - load in all details of them.	#
	#########################################################################
	my $s="";
	my @S=sort {$::GRI_FRONTEND->get_report_name($a) cmp $::GRI_FRONTEND->get_report_name($b)}  @R;
	my $row=0;
	for my $crep (@S) {
		my $class="gri_odd";
		$class="gri_even" if $row%2==0;
		my $type=$::GRI_FRONTEND->get_report_type($crep);
		my $name=$::GRI_FRONTEND->get_report_name($crep);
		$s.="<tr id=$class><td class=gri_cell>$name</td>";
		$s.="<td class=gri_cell-right>$type</td>";
		if(grep {$_ eq $crep} @L) {
			$s.="<td class=gri_cell-right><input type=checkbox name=\"RR_$crep\" value=1 checked=yes/></td>"
		} else {
			$s.="<td class=gri_cell-right><input type=checkbox name=\"RR_$crep\" value=1/></td>"
		}
		$s.="</tr>\n";
		$row++;
	}
	$s.="<input type=hidden name=portal value=\"$portal\"/><table class=grid cols=3>";
	return $s;
}


